<template>
	<view class="container">
		<!-- 顶部个人信息栏 -->
		<view class="user-info-bar">
			<view class="user-avatar">
				<image :src="userInfo.avatar" mode="aspectFit" v-if="userInfo && userInfo.avatar!=''"></image>
				<image :src="$imgUrl + '/static/avatar.png'" mode="aspectFit" v-else></image>
			</view>
			<view class="user-info">
				<text class="user-phone" v-if="userInfo && userInfo.nickname!=''">{{userInfo.nickname}}</text>
				<text class="user-type" v-if="userInfo.user_type == 0">个人版</text>
				<text class="user-type" v-if="userInfo.user_type == 1">商户版</text>
				<text class="user-type" v-if="userInfo.user_type == 2">门店版</text>
			</view>
			<!-- <view class="switch-identity">
				<image src="$imgUrl + '/static/qiehuan.png'" mode="aspectFit"></image>
				<text>切换身份</text>
			</view> -->
		</view>
		<!-- 功能按钮栏 -->
		<view class="function-buttons">
			<view class="button-item" @tap="functionutton(1)">
				<image :src="$imgUrl + '/static/coupon-icon.png'" mode="aspectFit"></image>
				<text>优惠券</text>
			</view>
			<view class="button-item" @tap="functionutton(2)">
				<image :src="$imgUrl + '/static/share-icon.png'" mode="aspectFit"></image>
				<text>分享推广</text>
			</view>
		</view>
		<!-- 常用功能列表 -->
		<view class="common-functions">
			<view class="section-title">常用功能</view>
			<view class="function-list">
				<view class="function-item" @tap="commonFunctions(1)">
					<image :src="$imgUrl + '/static/order-record-icon.png'" mode="aspectFit"></image>
					<text>外卖订单记录</text>
				</view>
				<view class="function-item" @tap="commonFunctions(2)">
					<image :src="$imgUrl + '/static/delivery-record-icon.png'" mode="aspectFit"></image>
					<text>送货记录</text>
				</view>
				<view class="function-item" @tap="commonFunctions(3)">
					<image :src="$imgUrl + '/static/message-icon.png'" mode="aspectFit"></image>
					<text>消息通知</text>
				</view>
				<view class="function-item" @tap="commonFunctions(4)">
					<image :src="$imgUrl + '/static/wallet-icon.png'" mode="aspectFit"></image>
					<text>钱包</text>
				</view>
				<view class="function-item" @tap="commonFunctions(5)">
					<image :src="$imgUrl + '/static/after-sale-icon.png'" mode="aspectFit"></image>
					<text>售后理赔</text>
				</view>
				<view class="function-item" @tap="commonFunctions(6)">
					<image src="/static/kefu.png" mode="aspectFit"></image>
					<text>客服</text>
				</view>
			</view>
		</view>
		<!-- 店铺管理列表 -->
		<view class="store-management">
			<view class="section-title">店铺管理</view>
			<view class="function-list">
				<view class="function-item" v-for="(item, index) in functionList" :key="index"
					@tap="handleFunction(index)">
					<image :src="item.src" mode="aspectFit"></image>
					<text>{{item.text}}</text>
				</view>
			</view>
		</view>
		<!-- 系统设置列表 -->
		<view class="system-settings">
			<view class="section-title">系统设置</view>
			<view class="function-list">
				<view class="function-item" @tap="version">
					<image :src="$imgUrl + '/static/version-icon.png'" mode="aspectFit"></image>
					<text>版本号</text>
				</view>
				<view class="function-item" @tap="removeUser">
					<image :src="$imgUrl + '/static/account-security-icon.png'" mode="aspectFit"></image>
					<text>注销账户</text>
				</view>
				<view class="function-item" @tap="changePassword">
					<image :src="$imgUrl + '/static/password-change-icon.png'" mode="aspectFit"></image>
					<text>登录密码修改</text>
				</view>
				<view class="function-item" @tap="doLogout">
					<image :src="$imgUrl + '/static/logout-icon.png'" mode="aspectFit"></image>
					<text>退出登录</text>
				</view>
			</view>
		</view>
		<!-- 用户协议和隐私政策 -->
		<view class="user-agreement">
			<text @click="goToUserAgreement(1)">《用户协议》</text>和
			<text @click="goToUserAgreement(2)">《隐私政策》</text>
		</view>
		<uni-popup ref="popupcenter" type="center">
			<view class="modal-container">
				<view class="modal-header">
					<uni-icons type="info-filled" size="45" color="red"></uni-icons>
				</view>
				<view class="modal-body">
					<view class="modal-title">警告</view>
					<view class="modal-message">请慎重考虑！注销后您的信息将清空且不能找回</view>
				</view>
				<view class="modal-footer">
					<button @click="hideModal" class="btnw">取消</button>
					<button @click="confirmAction" class="btnb">确认注销</button>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popupbottom" type="bottom" :safe-area="false">
			<view class="kefuContainer">
				<view class="header">
					<img src="/static/kefu.png" alt="" class="img" />
				</view>
				<view class="button-item">
					<button class="online" open-type="contact" @contact="onlineContact">在线客服</button>
					<button class="phone" @tap="callPhone">电话客服</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		logout,
		removeUser
	} from "@/api/userApi/user.js"
	export default {
		data() {
			return {
				isLogin: false,
				userInfo: {},
				functionList: [{
						src: this.$imgUrl + '/static/store-info-icon.png',
						text: '门店管理'
					},
					{
						src: this.$imgUrl + '/static/print-setting-icon.png',
						text: '打印设置'
					},
					{
						src: this.$imgUrl + '/static/staff-management-icon.png',
						text: '店员管理'
					},
					{
						src: this.$imgUrl + '/static/platform-bind-icon.png',
						text: '外卖平台绑定'
					},
					// {
					// 	src: this.$imgUrl + '/static/delivery-address-icon.png',
					// 	text: '发货地址'
					// },
					{
						src: this.$imgUrl + '/static/order-setting-icon.png',
						text: '发单设置'
					},
					{
						src: this.$imgUrl + '/static/message-setting-icon.png',
						text: '消息设置'
					},
					{
						src: this.$imgUrl + '/static/product-management-icon.png',
						text: '商品管理'
					},
				]
			}
		},
		methods: {
			functionutton(status) {
				if (status == 1) {
					uni.navigateTo({
						url: "/pagesUser/coupon/coupon"
					})
				}
				if (status == 2) {
					uni.navigateTo({
						url: "/pagesUser/share/share"
					})
				}
			},
			// 常用功能
			commonFunctions(status) {
				if (status == 1) {
					uni.switchTab({
						url: "/pages/order/order"
					})
				}
				if (status == 2) {
					uni.navigateTo({
						url: "/pagesOrder/deliveryRecord/deliveryRecord"
					})
				}
				if (status == 3) {
					uni.navigateTo({
						url: "/pagesUser/message/message"
					})
				}
				if (status == 4) {
					uni.navigateTo({
						url: "/pagesUser/wallet/wallet"
					})
				}
				if (status == 5) {
					uni.navigateTo({
						url: "/pagesUser/afterSales/afterSales"
					})
				}
				if (status == 6) {
					this.$refs.popupbottom.open()
				}
			},
			goToUserAgreement(value) {
				uni.navigateTo({
					url: `/pagesIndex/agreement/agreement?value=${value}`
				})
			},
			// 登出
			doLogout() {
				uni.showModal({
					title: "提示",
					content: "确定要退出登录？",
					success: (res) => {
						if (res.confirm) {
							logout().then(res => {
								uni.removeStorageSync("token")
								if (res.code == 200) {
									uni.reLaunch({
										url: "/pages/login/login"
									})
								}
							})
						}
					}
				})
			},
			changePassword() {
				uni.navigateTo({
					url: "/pagesUser/changePassword/changePassword"
				})
			},
			// 注销
			removeUser() {
				this.$refs.popupcenter.open();
			},
			hideModal() {
				this.$refs.popupcenter.close();
			},
			confirmAction() {
				removeUser().then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '注销成功',
							icon: 'none'
						});
						uni.removeStorageSync("token")
						uni.removeStorageSync("unserInfo")
						setTimeout(() => {
							uni.switchTab({
								url: "/pages/login/login"
							})
						}, 1000)
					}
				})
				this.hideModal();
			},
			handleFunction(index) {
				if (index == 0) {
					uni.navigateTo({
						url: "/pagesUser/storeManagement/storeManagement"
					})
				}
				if (index == 1) {
					uni.navigateTo({
						url: "/pagesUser/printer/printer"
					})
				}
				if (index == 2) {
					uni.navigateTo({
						url: "/pagesUser/userStoreList/userStoreList"
					})
				}
				if (index == 3) {
					uni.showToast({
						icon: "none",
						title: "暂未开放，敬请期待"
					})
				}
				if (index == 4) {
					uni.showToast({
						icon: "none",
						title: "暂未开放，敬请期待"
					})
				}
				if (index == 5) {
					uni.showToast({
						icon: "none",
						title: "暂未开放，敬请期待"
					})
				}
				if (index == 6) {
					uni.showToast({
						icon: "none",
						title: "暂未开放，敬请期待"
					})
				}
			},
			version() {
				const accountInfo = wx.getAccountInfoSync();
				let version = accountInfo.miniProgram.version;
				if (version == "") {
					version = "1.08";
				}
				uni.showToast({
					icon: "none",
					title: version
				});
			},
			onlineContact(e) {
				console.log(e);
			},
			callPhone() {
				uni.makePhoneCall({
					phoneNumber: "400-668-9397",
					success: (res) => {
						console.log(res);
					},
					fail: (err) => {
						console.log(err);
					}
				})
			}
		},
		onShow() {
			this.userInfo = uni.getStorageSync("unserInfo")
		}
	}
</script>
<style lang="scss">
	.container {
		padding: 20rpx;
		background: linear-gradient(180deg, #E0EEFF 0%, #F5F5F5 52%);
	}

	.user-info-bar {
		display: flex;
		align-items: center;
		padding: 20rpx;
		border-radius: 20rpx;
		margin-bottom: 30rpx;

		.user-avatar {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-right: 20rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.user-info {
			flex: 1;

			.user-phone {
				font-size: 32rpx;
				font-weight: bold;
			}

			.user-type {
				display: inline-block;
				padding: 5px 10px;
				background-color: #3399ff;
				color: #fff;
				border-radius: 20px;
				font-size: 14px;
				margin-left: 10rpx;
			}
		}

		.switch-identity {
			padding: 10rpx 20rpx;
			border-radius: 30rpx;
			color: #3399ff;
			font-size: 28rpx;
			display: flex;
			align-items: center;
			background-color: #fff;

			image {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}

	.function-buttons {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;

		.button-item {
			display: flex;
			align-items: center;
			padding: 20rpx;
			background-color: #fff;
			border-radius: 20rpx;
			flex: 1;
			margin-right: 20rpx;

			image {
				width: 70rpx;
				height: 77rpx;
				margin-right: 10rpx;
			}

			text {
				font-size: 32rpx;
				color: #333333;
			}

			&:last-child {
				margin-right: 0;
			}
		}
	}

	.common-functions,
	.store-management,
	.system-settings {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;

		.section-title {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.function-list {
			display: flex;
			flex-wrap: wrap;

			.function-item {
				width: 25%;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-bottom: 20rpx;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-bottom: 10rpx;
				}

				text {
					font-size: 24rpx;
				}
			}
		}
	}

	.user-agreement {
		text-align: center;
		font-size: 24rpx;
		color: #3399ff;
		margin-bottom: 20rpx;
	}

	.modal-container {
		width: 300px;
		background-color: #fff;
		border-radius: 10px;
		padding: 20px;
		text-align: center;

		.modal-header {
			margin-bottom: 20px;
		}

		.modal-title {
			font-size: 18px;
			font-weight: bold;
			margin-bottom: 10px;
		}

		.modal-message {
			font-size: 14px;
			color: #666;
			margin-bottom: 20px;
		}

		.modal-footer {
			display: flex;
			justify-content: space-around;

			.btnw {
				width: 46%;
				height: 94rpx;
				border-radius: 88rpx 88rpx 88rpx 88rpx;
				border: 2rpx solid rgba(153, 153, 153, 0.38);
				display: flex;
				justify-content: center;
				align-items: center;
				font-weight: 500;
				font-size: 34rpx;
				color: #666666;
			}

			.btnb {
				width: 46%;
				height: 94rpx;
				background: #ff0000;
				border-radius: 132rpx 132rpx 132rpx 132rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-weight: 500;
				font-size: 34rpx;
				color: #FFFFFF;
			}
		}
	}

	.kefuContainer {
		background-color: #fff;
		border-radius: 10px;
		padding: 20px;
		text-align: center;

		.header {
			display: flex;
			justify-content: center;
			align-items: center;

			.img {
				width: 150rpx;
				height: 120rpx;
			}
		}

		.button-item {
			margin-top: 40rpx;

			.online {}

			.phone {
				margin-top: 30rpx;
			}
		}
	}
</style>